
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.*;
/*
 * Created by JFormDesigner on Tue Jul 31 16:17:51 EDT 2012
 */


/**
 * @author james ju
 */
public class Capture_GUI extends JPanel {
	
	private static final long serialVersionUID = 1L;
	private utils util = new utils();
	private CaptureThread ct;
	private boolean start_capture = false;
	private Packet_Builder pb;
	private boolean clicked_capture = false;
	private jpcap.NetworkInterface[] neti;
	private int Interface_count = 0;
	private int Interface_selected;
	public Capture_GUI() {		
		neti = jpcap.JpcapCaptor.getDeviceList();		
		Interface_selected = 0;
		initComponents();
		for(jpcap.NetworkInterface i :neti)
		{
			textArea1.append("Network Interface "+ Interface_count + ": " + i.description + "\n");
			Interface_count++;
		}
	}

	private void menuItem2ActionPerformed(ActionEvent e) {
		// TODO add your code here
		System.exit(0);
	}

	private void menuItem3ActionPerformed(ActionEvent e) {
		// TODO add your code here	
	}

	private void startActionPerformed(ActionEvent e) {
		start_capture = true;
		
		startCapt();
	}

	private void stopActionPerformed(ActionEvent e) {
		start_capture = false;
	}

	private void button2ActionPerformed(ActionEvent e) {
		// TODO add your code here
	}
	public void startCapt()
	{	
		ct = new CaptureThread()
		{
			public Object construct()
			{
				try
				{
					while(start_capture)
					{
						try
						{
							Interface_selected = Integer.parseInt(NIC_SELECT.getText());
						}catch(java.lang.NumberFormatException e){ NIC_SELECT.setText(Integer.toString(Interface_selected)); }
						
						if(ct.startcapture(neti[Interface_selected])==1)
						{
							pb = ct.getRead_data();
							switch(filters.getSelectedIndex())
							{
								case 1: if(filter_field.getText().compareTo(ct.getRead_data().ep.getDestinationAddress())==0)
										appendTotextArea(); 
										break;
								case 2: if(filter_field.getText().compareTo(ct.getRead_data().ep.getSourceAddress())==0)
										appendTotextArea(); 
										break;
								case 3:if(filter_field.getText().compareTo(Integer.toHexString(ct.getRead_data().frametype))==0)
										appendTotextArea(); 
										break;
								case 4:if((filter_field.getText().compareTo(ct.getRead_data().ep.getDestinationAddress())==0)
										||(filter_field.getText().compareTo(ct.getRead_data().ep.getSourceAddress())==0))
										appendTotextArea(); 
										break;
								default :appendTotextArea(); 
							}
							
						}						
					}
				}catch(Exception e){ System.out.println(e); }
				
				return 0;
			}
			public void finished()
			{
				ct.interrupt();
			}
		};		
		ct.start();
	}
	
	private void frame1WindowClosing(WindowEvent e) {
		int select = JOptionPane.showConfirmDialog(this, "Are you sure you want to exit ?","Exit",JOptionPane.YES_NO_OPTION);
		if(select == JOptionPane.YES_OPTION)
				System.exit(0);
	}

	private void sendActionPerformed(ActionEvent e) {
				try {
					String protocol_Type;
					switch(send_Filter.getSelectedIndex())
					{
						case 0 : protocol_Type ="TCP"; break;
						case 1 : protocol_Type = "UDP"; break;
						case 2 : protocol_Type ="ARP"; break;
						default : protocol_Type = "TCP";
					}
					ct.sendPacket(neti[Interface_selected], Mac_Address_tArea.getText(),protocol_Type,
							Integer.parseInt(src_port.getText()),Integer.parseInt(dst_port.getText()));
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
	}

	private void NICActionPerformed(ActionEvent e) {
		Interface_count = 0;
		for(jpcap.NetworkInterface i :neti)
		{
			textArea1.append("Network Interface "+ Interface_count + ": " + i.description + "\n");
			Interface_count++;
		}
	}

	private void selectedActionPerformed(ActionEvent e) 
	{
		try
		{
			if(Integer.parseInt(NIC_SELECT.getText()) < Interface_count)
			{
				Interface_selected = Integer.parseInt(NIC_SELECT.getText());
				textArea1.append("Interface " +NIC_SELECT.getText() + " has been selected\n");
			}
			else
				textArea1.append("Non-Existing NIC Value\n");
		}catch(NumberFormatException i){ NIC_SELECT.setText(Integer.toString(Interface_selected)); }
	}
	
	private void Mac_Address_tAreaCaretUpdate(CaretEvent e) {		
		//if((Mac_Address_tArea.getText().length()%2)==0 && Mac_Address_tArea.getText().length()!=0)
		if(Mac_Address_tArea.getText().length()==18)
		SwingUtilities.invokeLater(new Runnable() {
            public void run() {   	System.out.println("Invalid length");    }             
		});
		}

	public void appendTotextArea()
	{
		textArea1.append("*******************************************START PACKET*******************************************\n");
		textArea1.append("*******************************************ETHERNET*************************************************\n");
		textArea1.append("Source MAC: " + ct.getRead_data().ep.getSourceAddress() + "\n");
		textArea1.append("Destination MAC: " + ct.getRead_data().ep.getDestinationAddress() + "\n");						
		textArea1.append("Frame Type: " + "0x" + Integer.toHexString(ct.getRead_data().ep.frametype) + "\n");
		if(Integer.toHexString(ct.getRead_data().frametype).compareTo("800")==0) //Frame 0x800 = TCP/IP : UDP Type
		{	
			textArea1.append("***********************************INTERNET PROTOCOL VERSION 4 HEADER**********************************\n");
			textArea1.append("Source IP: " + ct.getRead_data().src_ip.getHostAddress() + "\n");
			textArea1.append("Source Port: " + ct.getRead_data().src_port + "\n");
			textArea1.append("Destination IP: " + ct.getRead_data().dst_ip.getHostAddress() + "\n");
			textArea1.append("Destination Port: " + ct.getRead_data().dst_port + "\n");
			textArea1.append("Version: " + ct.getRead_data().version + "\n");
			textArea1.append("TOS: " + ct.getRead_data().rsv_tos + "\n");
			textArea1.append("Total Length: " + ct.getRead_data().length + "\n");
			textArea1.append("Identification: " + ct.getRead_data().ident + "\n");
			textArea1.append("Dont Frag: " + ct.getRead_data().dont_frag +"   " + "More Frag: " + ct.getRead_data().more_frag +"\n");
			textArea1.append("Frag Offset: " + ct.getRead_data().offset + "\n");
			textArea1.append("TTL: " + ct.getRead_data().hop_limit + "\n");
			textArea1.append("Protocol: " + ct.getRead_data().protocol + "\n");
			textArea1.append("IPV4 Checksum: " + ct.getRead_data().checksum_ipv4 + "\n");
			if(ct.getRead_data().protocol==6)
			{
				textArea1.append("*******************************************TCP/IP HEADER*******************************************\n");
				textArea1.append("Seq Num: " + ct.getRead_data().sequence + "\n");
				textArea1.append("Ack Num: " + ct.getRead_data().ack_num + "\n");
				textArea1.append("Flags [URG: " + ct.getRead_data().urg +" ACK:"+ct.getRead_data().ack+" PSH:"+ct.getRead_data().psh+ " RST:"+ct.getRead_data().psh
						+" SYN:" +ct.getRead_data().syn + " FIN:" +ct.getRead_data().fin+"]\n");
				textArea1.append("Window Size: " + ct.getRead_data().window + "\n");			
				textArea1.append("TCP/IP Checksum: " + ct.getRead_data().checksum_tcp + "\n");
			}else
			{
				textArea1.append("*******************************************UDP HEADER*******************************************\n");
				textArea1.append("UDP Length: " + ct.getRead_data().udp_Length + "\n");
				textArea1.append("UDP Checksum: " + ct.getRead_data().checksum_udp + "\n");
			}
			textArea1.append("Header Hexadec : " + util.toReadbleHex(ct.getRead_data().pkt.header) + "\n");
			textArea1.append("Data Hexadec : " + util.toReadbleHex(ct.getRead_data().pkt.data) + "\n");
			textArea1.append("Header size : " + ct.getRead_data().pkt.header.length + "\n");
			textArea1.append("Data size : " + ct.getRead_data().pkt.data.length + "\n");			
		}
		if(Integer.toHexString(ct.getRead_data().ep.frametype).compareTo("806")==0)//Frame 0x806 = ARP Type
		{
			textArea1.append("*******************************************ARP*******************************************\n");
			textArea1.append("Hardware Type : " + ct.getRead_data().hardtype + "\n");
			textArea1.append("Protocol Type : " + ct.getRead_data().protocol + "\n");
			textArea1.append("Hardware Size : " + ct.getRead_data().hlen + "\n");
			textArea1.append("Protocol Size : " + ct.getRead_data().plen + "\n");
			textArea1.append("Opcode : " + ct.getRead_data().operation + "\n");
			textArea1.append("Senders MAC Address : " + util.ConvertoMACAddress(ct.getRead_data().sender_hardaddr) + "\n");
			textArea1.append("Senders IP Address : " + util.ConvertIPAddress((ct.getRead_data().sender_protoaddr)) + "\n");
			textArea1.append("Target MAC Address : " + util.ConvertoMACAddress(ct.getRead_data().target_hardaddr) + "\n");
			textArea1.append("Target IP Address : " + util.ConvertIPAddress(ct.getRead_data().target_protoaddr) + "\n");
			textArea1.append("Header Hexadec : " + util.toReadbleHex(ct.getRead_data().pkt.header) + "\n");
			textArea1.append("Header size : " + ct.getRead_data().pkt.header.length + "\n");
			
			
		}		
		textArea1.setCaretPosition(textArea1.getDocument().getLength());			
		textArea1.append("*******************************************End of Packet*******************************************\n\n");
	}
	
	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		// Generated using JFormDesigner non-commercial license
		frame1 = new Frame();
		toolBar1 = new JToolBar();
		menuBar2 = new JMenuBar();
		menu1 = new JMenu();
		menuItem3 = new JMenuItem();
		menuItem1 = new JMenuItem();
		menuItem2 = new JMenuItem();
		scrollPane1 = new JScrollPane();
		textArea1 = new JTextArea();
		panel1 = new JPanel();
		start = new JButton();
		stop = new JButton();
		label1 = new JLabel();
		label4 = new JLabel();
		NIC_SELECT = new JTextField();
		filters = new JComboBox<>();
		send_Filter = new JComboBox<>();
		Mac_Address_tArea = new JTextField();
		selected = new JButton();
		filter_field = new JTextField();
		label3 = new JLabel();
		src_port = new JTextField();
		send = new JButton();
		NIC = new JButton();
		label2 = new JLabel();
		dst_port = new JTextField();

		//======== frame1 ========
		{
			frame1.setVisible(true);
			frame1.addWindowListener(new WindowAdapter() {
				@Override
				public void windowClosing(WindowEvent e) {
					frame1WindowClosing(e);
				}
			});
			frame1.setLayout(new BorderLayout());

			//======== this ========
			{
				this.setLayout(new BorderLayout());

				//======== toolBar1 ========
				{

					//======== menuBar2 ========
					{

						//======== menu1 ========
						{
							menu1.setText("File");

							//---- menuItem3 ----
							menuItem3.setText("Open");
							menuItem3.addActionListener(new ActionListener() {
								@Override
								public void actionPerformed(ActionEvent e) {
									menuItem3ActionPerformed(e);
								}
							});
							menu1.add(menuItem3);

							//---- menuItem1 ----
							menuItem1.setText("Save");
							menu1.add(menuItem1);

							//---- menuItem2 ----
							menuItem2.setText("Exit");
							menuItem2.addActionListener(new ActionListener() {
								@Override
								public void actionPerformed(ActionEvent e) {
									menuItem2ActionPerformed(e);
								}
							});
							menu1.add(menuItem2);
						}
						menuBar2.add(menu1);
					}
					toolBar1.add(menuBar2);
				}
				this.add(toolBar1, BorderLayout.NORTH);

				//======== scrollPane1 ========
				{
					scrollPane1.setAutoscrolls(true);

					//---- textArea1 ----
					textArea1.setEditable(false);
					scrollPane1.setViewportView(textArea1);
				}
				this.add(scrollPane1, BorderLayout.CENTER);

				//======== panel1 ========
				{
					panel1.setLayout(new GridBagLayout());
					((GridBagLayout)panel1.getLayout()).columnWidths = new int[] {0, 0, 5, 5, 23, 5, 21, 0, 5, 0, 5, 28, 5, 0, 5, 0, 5, 0, 5, 12, 5, 27, 21, 5, 5, 0, 5, 0, 5, 25, 5, 26, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 0};
					((GridBagLayout)panel1.getLayout()).rowHeights = new int[] {0, 0, 5, 0, 5, 0, 0};
					((GridBagLayout)panel1.getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};
					((GridBagLayout)panel1.getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};

					//---- start ----
					start.setText("Start");
					start.setMaximumSize(new Dimension(73, 23));
					start.setMinimumSize(new Dimension(73, 23));
					start.setPreferredSize(new Dimension(73, 23));
					start.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent e) {
							startActionPerformed(e);
						}
					});
					panel1.add(start, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(0, 0, 0, 0), 0, 0));

					//---- stop ----
					stop.setText("Stop");
					stop.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent e) {
							button2ActionPerformed(e);
							stopActionPerformed(e);
						}
					});
					panel1.add(stop, new GridBagConstraints(5, 0, 1, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(0, 0, 0, 0), 0, 0));

					//---- label1 ----
					label1.setText("Filter");
					label1.setHorizontalAlignment(SwingConstants.CENTER);
					panel1.add(label1, new GridBagConstraints(7, 0, 3, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(0, 0, 0, 0), 0, 0));

					//---- label4 ----
					label4.setText("PACKET SEND");
					label4.setHorizontalAlignment(SwingConstants.CENTER);
					panel1.add(label4, new GridBagConstraints(25, 0, 9, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(0, 0, 0, 0), 0, 0));

					//---- NIC_SELECT ----
					NIC_SELECT.setText("0");
					NIC_SELECT.setHorizontalAlignment(SwingConstants.CENTER);
					panel1.add(NIC_SELECT, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(0, 0, 0, 0), 0, 0));

					//---- filters ----
					filters.setModel(new DefaultComboBoxModel<>(new String[] {
						"All",
						"dst_ip",
						"src_ip",
						"frametype",
						"dst_ip || src_ip"
					}));
					panel1.add(filters, new GridBagConstraints(7, 1, 9, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(0, 0, 0, 0), 0, 0));

					//---- send_Filter ----
					send_Filter.setModel(new DefaultComboBoxModel<>(new String[] {
						"TCP/IPV4",
						"UDP",
						"ARP"
					}));
					panel1.add(send_Filter, new GridBagConstraints(25, 1, 1, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(0, 0, 0, 0), 0, 0));

					//---- Mac_Address_tArea ----
					Mac_Address_tArea.setText("00:19:03:04:21:33");
					Mac_Address_tArea.setMinimumSize(new Dimension(98, 20));
					Mac_Address_tArea.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
					Mac_Address_tArea.setPreferredSize(new Dimension(150, 20));
					Mac_Address_tArea.addCaretListener(new CaretListener() {
						@Override
						public void caretUpdate(CaretEvent e) {
							Mac_Address_tAreaCaretUpdate(e);
						}
					});
					panel1.add(Mac_Address_tArea, new GridBagConstraints(27, 1, 7, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(0, 0, 0, 0), 0, 0));

					//---- selected ----
					selected.setText("Select");
					selected.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent e) {
							selectedActionPerformed(e);
						}
					});
					panel1.add(selected, new GridBagConstraints(3, 3, 1, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(0, 0, 0, 0), 0, 0));

					//---- filter_field ----
					filter_field.setText("00:19:03:04:21:33");
					panel1.add(filter_field, new GridBagConstraints(7, 3, 9, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(0, 0, 0, 0), 0, 0));

					//---- label3 ----
					label3.setText("Src Port");
					label3.setHorizontalAlignment(SwingConstants.CENTER);
					panel1.add(label3, new GridBagConstraints(25, 3, 1, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(0, 0, 0, 0), 0, 0));

					//---- src_port ----
					src_port.setText("3308");
					src_port.setMinimumSize(new Dimension(34, 22));
					panel1.add(src_port, new GridBagConstraints(27, 3, 5, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(0, 0, 0, 0), 0, 0));

					//---- send ----
					send.setText("Send");
					send.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent e) {
							sendActionPerformed(e);
						}
					});
					panel1.add(send, new GridBagConstraints(33, 3, 1, 3, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(0, 0, 0, 0), 0, 0));

					//---- NIC ----
					NIC.setText("NICLIST");
					NIC.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent e) {
							NICActionPerformed(e);
						}
					});
					panel1.add(NIC, new GridBagConstraints(3, 5, 1, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(0, 0, 0, 0), 0, 0));

					//---- label2 ----
					label2.setText("Dst Port");
					label2.setHorizontalAlignment(SwingConstants.CENTER);
					panel1.add(label2, new GridBagConstraints(25, 5, 1, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(0, 0, 0, 0), 0, 0));

					//---- dst_port ----
					dst_port.setMinimumSize(new Dimension(34, 22));
					dst_port.setPreferredSize(new Dimension(34, 22));
					panel1.add(dst_port, new GridBagConstraints(27, 5, 5, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(0, 0, 0, 0), 0, 0));
				}
				this.add(panel1, BorderLayout.SOUTH);
			}
			frame1.add(this, BorderLayout.CENTER);
			frame1.setSize(735, 505);
			frame1.setLocationRelativeTo(null);
		}
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}
	
	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	// Generated using JFormDesigner non-commercial license
	private Frame frame1;
	private JToolBar toolBar1;
	private JMenuBar menuBar2;
	private JMenu menu1;
	private JMenuItem menuItem3;
	private JMenuItem menuItem1;
	private JMenuItem menuItem2;
	private JScrollPane scrollPane1;
	private JTextArea textArea1;
	private JPanel panel1;
	private JButton start;
	private JButton stop;
	private JLabel label1;
	private JLabel label4;
	private JTextField NIC_SELECT;
	private JComboBox<String> filters;
	private JComboBox<String> send_Filter;
	private JTextField Mac_Address_tArea;
	private JButton selected;
	private JTextField filter_field;
	private JLabel label3;
	private JTextField src_port;
	private JButton send;
	private JButton NIC;
	private JLabel label2;
	private JTextField dst_port;
	// JFormDesigner - End of variables declaration  //GEN-END:variables

	public static void main(String args[])
	{
		new Capture_GUI().setVisible(true);		
	}
}
